package edu.mcw.rgd.clinminer.web.utils;

import com.vaadin.ui.TextField;

import com.vaadin.data.Validator;
import com.vaadin.terminal.CompositeErrorMessage;
import com.vaadin.terminal.ErrorMessage;
import com.vaadin.terminal.UserError;

public class TextFieldEx extends TextField {
	@Override
	public ErrorMessage getErrorMessage() {
		/*
		 * Check validation errors only if automatic validation is enabled.
		 * Empty, required fields will generate a validation error containing
		 * the requiredError string. For these fields the exclamation mark will
		 * be hidden but the error must still be sent to the client.
		 */
		ErrorMessage validationError = null;
		if (isValidationVisible()) {
			try {
				validate();
			} catch (Validator.InvalidValueException e) {
				if (!e.isInvisible()) {
					validationError = e;
				}
			}
		}

		// Check if there are any systems errors
		ErrorMessage superError = super.getErrorMessage();

		if (superError != null) {
			superError = new UserError("Field invalid");
		}

		// Return if there are no errors at all
		if (superError == null && validationError == null) {
			return null;
		}

		// Throw combination of the error types
		return new CompositeErrorMessage(new ErrorMessage[] {
				superError,
				validationError });
	}

}
